package www.openteam.com.model.business;

import android.os.Trace;

import www.openteam.com.OTApplication;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.ApprovalStatu;
import www.openteam.com.bean.CustomOrder;
import www.openteam.com.exception.ApiException;
import www.openteam.com.subscribe.CommonSubscriber;
import www.openteam.com.transformer.CommonTransformer;

/**
 * Created by kylin on 2016/11/14.
 */

public class QueryCustomOrderInfoModel extends BaseModel {
    private boolean isQuery = false;
    private boolean isUpdate;
    private boolean isInsert;

    public boolean queryCustomOrderInfo(String rdsno,
                                        String custid,
                                        int pageSize, final QueryCustomOrderInfoOnListener queryCustomOrderInfoOnListener) {

        httpApiService.queryCustomOrderInfo(rdsno, custid, pageSize)
                .compose(new CommonTransformer<CustomOrder>())
                .subscribe(new CommonSubscriber<CustomOrder>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(CustomOrder customOrder) {
                        isQuery = true;
                        queryCustomOrderInfoOnListener.queryCOISuccess(customOrder);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isQuery = false;
                        queryCustomOrderInfoOnListener.queryCOIFail(e);
                    }
                });

        return isQuery;

    }
    public boolean updateCustomOrderMainInfo(
            int id, String rdsno,
            String custid, String userid,
            String moneytype, String paytime
            , String sendtype, String custorderid
            , String alreadlypay, String inputdate
            , String inputer, String orderamount
            , String remark, String orderdate
            , String sendaddress, final String istax
            , String taxrate, String invtype
            , String stype, final UpdateCustomOrderInfoOnListener updateCustomOrderInfoOnListener
    ) {
        httpApiService.updateCustomOrderMainInfo(id, rdsno, custid, userid, moneytype, paytime, sendtype, custorderid, alreadlypay, inputdate, inputer, orderamount, remark, orderdate, sendaddress, istax, taxrate, invtype, stype)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        updateCustomOrderInfoOnListener.updateCOISuccess(approvalStatu);
                        isUpdate = true;
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        updateCustomOrderInfoOnListener.updateCOIFail(e);
                        isUpdate = false;
                    }
                });

        return isUpdate;
    }

    //新建主订单
    public boolean insertCustomOrderMainInfo(
            String custid, String businessno,
            String userid,
            String moneytype, String paytime
            , String sendtype, String custorderid
            , String alreadlypay, String inputdate
            , String inputer, String orderamount
            , String remark, String orderdate
            , String sendaddress, String istax
            , String taxrate, String invtype
            , String stype, final InsertCustomOrderInfoOnListener insertCustomOrderInfoOnListener
    ) {
        httpApiService.insertCustomOrderMainInfo(custid, businessno, userid, moneytype, paytime, sendtype, custorderid, alreadlypay, inputdate, inputer, orderamount, remark, orderdate, sendaddress, istax, taxrate, invtype, stype)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isInsert = true;
                        insertCustomOrderInfoOnListener.insertCOISuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isInsert = false;
                        insertCustomOrderInfoOnListener.insertdCOIFail(e);
                    }
                });
        return isInsert;
    }

    public interface QueryCustomOrderInfoOnListener {
        void queryCOISuccess(CustomOrder customOrder);

        void queryCOIFail(Throwable throwable);
    }
    //更新主订单
    public interface UpdateCustomOrderInfoOnListener {

        void updateCOISuccess(ApprovalStatu updateState);

        void updateCOIFail(Throwable throwable);
    }

    //插入主订单
    public interface InsertCustomOrderInfoOnListener {
        void insertCOISuccess(ApprovalStatu approvalStatu);

        void insertdCOIFail(Throwable throwable);

    }

}
